#include <pthread.h>
#include "MsgPubSub.h"

MsgCenterHandle_t *handle = NULL;

#define TOPIC_1 "test"
#define TOPIC_2 "test1"
#define TOPIC_3 "test2"
#define TOPIC_4 "test3"

void HandleEvent1(void *data, int srcId)
{
   MSG_CENTER_LOG("data: %s srcId: %d \r\n", (char *)data, srcId);
}


void *task1(void *usrdata)
{
     int cnt = 0;
     SubscriberEvent(handle, TOPIC_1, HandleEvent1, 0);
	 SubscriberEvent(handle, TOPIC_1, HandleEvent1, 0);
	 SubscriberEvent(handle, TOPIC_2, HandleEvent1, 1);
	 SubscriberEvent(handle, TOPIC_3, HandleEvent1, 2);
	 SubscriberEvent(handle, TOPIC_4, HandleEvent1, ANY_ID);
	 while(1)
	 	{
	 	  
          sleep(1);
		  
		  cnt++;
		  if(cnt == 1)
		  	{
              ReleaseSubscriberEvent(handle, TOPIC_4, HandleEvent1);
		  }
		  else if(cnt == 2)
		  	{
              ReleaseSubscriberEvent(handle, TOPIC_3, HandleEvent1);
		  }
		   else if(cnt == 3)
		  	{
              ReleaseSubscriberEvent(handle, TOPIC_2, HandleEvent1);
		  }
		    else if(cnt == 4)
		  	{
              ReleaseSubscriberEvent(handle, TOPIC_1, HandleEvent1);
		  }
		   else if(cnt == 5)
		  	{
             SubscriberEvent(handle, TOPIC_1, HandleEvent1, 0);
		   	}
		    else if(cnt == 6)
		  	{
            SubscriberEvent(handle, TOPIC_2, HandleEvent1, 1);
		   	}
			 else if(cnt == 7)
		  	{
            SubscriberEvent(handle, TOPIC_3, HandleEvent1, 2);
		   	}

			  else if(cnt == 8)
		  	{
            SubscriberEvent(handle, TOPIC_4, HandleEvent1, ANY_ID);
		   	}
			else if(cnt == 10)
			  	{
                  // MsgCenterDeinit(&handle);
			 }
	 }
}

void *task2(void *usrdata)
{
     while(1)
	 	{
          PublishEvent(handle, TOPIC_1, "qwert", 5, 0);
		  PublishEvent(handle, TOPIC_2, "qwert1", 6, 1);
		  PublishEvent(handle, TOPIC_3, "qwert2", 6, 2);
		  PublishEvent(handle, TOPIC_4, "qwert3", 6, 6); 
		  PublishEvent(handle, TOPIC_4, "qwert3", 6, 7); 
          sleep(1);
	 }
}

void *task3(void *usrdata)
{
	  while(1)
		   {
			 PublishEvent(handle, TOPIC_1, "qwert", 5, 0);
			 PublishEvent(handle, TOPIC_2, "qwert1", 6, 1);
			 PublishEvent(handle, TOPIC_3, "qwert2", 6, 2);
			 PublishEvent(handle, TOPIC_4, "qwert3", 6, 6); 
			 PublishEvent(handle, TOPIC_4, "qwert3", 6, 7); 
			 sleep(2);
		}


}

void *task4(void *usrdata)
{
	  while(1)
		   {
			  SubscriberEvent(handle, TOPIC_1, HandleEvent1, 0);
			 SubscriberEvent(handle, TOPIC_1, HandleEvent1, 0);
			 SubscriberEvent(handle, TOPIC_2, HandleEvent1, 1);
			 SubscriberEvent(handle, TOPIC_3, HandleEvent1, 2);
			 SubscriberEvent(handle, TOPIC_4, HandleEvent1, ANY_ID);
			 sleep(3);
		}


}


int main(void)
{
   handle = MsgCenterInit();
   if(handle == NULL)
   	{
   	   MSG_CENTER_LOG("msg center init fail\r\n");
       return 0;
   }
   #if 1
   pthread_t pt_id;
   pthread_t pt_id1;
   pthread_t pt_id2;
   pthread_t pt_id3;
   pthread_create(&pt_id, NULL, task1, NULL);
   pthread_detach(pt_id);
   pthread_create(&pt_id1, NULL, task2, NULL);
   pthread_detach(pt_id1);
     pthread_create(&pt_id2, NULL, task3, NULL);
   pthread_detach(pt_id2);
        pthread_create(&pt_id3, NULL, task4, NULL);
   pthread_detach(pt_id3);
   #endif
   while(1)
   {
      sleep(1);
   }
   return 0;
}
